package com.stock.demo.framework.ui.view;

/**
 * Created by XuZhaoyu on 2017/5/22.
 */

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.stock.demo.R;
import com.stock.demo.framework.ui.TipType;

public class TipToast {

    private Toast mToast;
    private final Context mAppContext;
    private View mToastRoot;

    public TipToast(Context context) {
        mAppContext = context.getApplicationContext();
        if (mToast == null) {
            mToast = new Toast(mAppContext);
        }
    }

    public void showToast(TipType type, String message) {
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.setView(createToastView(mAppContext, message, type));
        mToast.show();
    }

    /**
     * 生成Toast的布局
     *
     * @param context 上下文
     * @param message 提示的内容
     * @param type    类型：NONE-隐藏图片 ERROR-错误图片 SUCCESS-成功图片
     * @return
     */
    private View createToastView(Context context, String message, TipType type) {
        mToastRoot = LayoutInflater.from(context).inflate(R.layout.layout_toast_tip, null);
        TextView tvTitle = (TextView) mToastRoot.findViewById(R.id.tv_message);
        ImageView ivType = (ImageView) mToastRoot.findViewById(R.id.iv_type);
        switch (type) {
            case NONE:
                ivType.setVisibility(View.GONE);
                break;
            case ERROR:
                ivType.setImageResource(R.mipmap.tip_toast_error);
                break;
            case SUCCESS:
                ivType.setImageResource(R.mipmap.tip_toast_success);
                break;
        }
        tvTitle.setText(message);
        return mToastRoot;
    }
}